<?php

/**
 * ECSHOP 用户中心
 * ============================================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: user.php 16643 2009-09-08 07:02:13Z liubo $
*/

define('IN_ECS', true);
define('ECS_ADMIN', true);

require(dirname(__FILE__) . '/includes/init.php');
require_once(ROOT_PATH . '/includes/oss-sdk/Object.php');
/* 载入语言文件 */
require_once(ROOT_PATH . 'languages/' .$_CFG['lang']. '/user.php');
//if ($_SESSION['user_id'] > 0)
//{
//	$smarty->assign('user_name', $_SESSION['user_name']);
//}
$act = isset($_GET['act']) ? $_GET['act'] : '';
//$user_id = $_SESSION['user_id'];

/* 用户登陆 */
if ($act == 'app_login')
{
	$login_name = !empty($_GET['uname']) ? $_GET['uname'] : '';
	$pwd = !empty($_GET['pwd']) ? $_GET['pwd'] : '';
//	$gourl = !empty($_POST['gourl']) ? $_POST['gourl'] : '';
	$log_id = $user->check_user($login_name, $pwd);
	if (!$log_id)
	{//再次判断是否自动登录
		$auto_login = check_auto_login($login_name, $pwd);
		if ($auto_login)	$log_id = $auto_login;
	}
	if ($log_id)
	{
		$uu['error']=0;		
		if ($GLOBALS['db']->getOne("select step from".$GLOBALS['ecs']->table('users')." where user_id = '$log_id'"))
		{
			$uu['step']=1;						
			$uu['message']="登录成功";
			$uu['uinfo']=get_user_json($log_id,$login_name,1);			
		}
		else
		{
			$uu['step']=0;			
			$uu['message']="请补齐注册信息";			
		}
		echo json_encode($uu);
	}else{
		$uu['message']="登录失败";
		$uu['error']=1;
		echo json_encode($uu);
	}
}


/* 微信第三方登陆 */
elseif ($act == 'weixin_login')
{
	require_once(dirname(__FILE__) . '/auth/class_weixin_adv.php');
	include_once(ROOT_PATH . 'includes/lib_passport.php');	
	
//	$_REQUEST['code'] = "0415cbe3e5a704c0c5b9b69b08489e0i";
	
	$result['error']=1;	
	
	$weixin=new class_weixin_adv();
	if (isset($_REQUEST['code'])){
        $res = $weixin->get_token($_REQUEST['code']);
		if (isset($res['openid']))
		{
			$row=$weixin->get_user_info();	
			if (isset($row['unionid']))
			{
				$result['error']=0;
				//查询是否已有账户
				$user_id = $db->getOne("SELECT user_id FROM " .$ecs->table('users'). " WHERE auth_wechat = '".$row['unionid']."'");
				if ($user_id){
					//直接登录
					$result['state']=1;
					$result['uinfo']=get_user_json($user_id,$row['unionid'],1);		
					$result['msg']="登录成功";					
				}
				else{
					//新用户注册
					$result['state']=2;					
					$username = $row['unionid'];
					$password = $row['unionid'];		
					$uu = app_register($username, $password);
					if ($uu['error']==0)			
					{
						$sql=" update ".$GLOBALS['ecs']->table('users')." set auth_wechat='".$row['unionid']."' where user_name = '".$row['unionid']."'";
						$GLOBALS['db']->query($sql);
						//填充用户信息						
						$user_id = $db->getOne("SELECT user_id FROM " .$ecs->table('users'). " where user_name = '".$row['unionid']."'");
						$result['uinfo']=get_user_json($user_id,$username,1);
						$result['uinfo']['user_nick']=$row['nickname'];
						$result['uinfo']['avatar']=substr($row['headimgurl'],0,strripos($row['headimgurl'],"/")+1)."132";
						$result['uinfo']['sex']=$row['sex'];						
					}
					else
					{
						$result['error']=1;
						$result['msg']=$uu["message"];
					}
				}	
			}
			else
			{
				$result['error']=1;					
				$result['msg']=$row;							
			}
		}
		else
		{
			$result['msg']=$res;			
		}
    }else{
		$result['msg']="NO CODE";
    }
	echo json_encode($result);	
}

/* qq第三方登陆 */
elseif ($act == 'qq_login')
{
	require_once(dirname(__FILE__) . '/auth/class_qq_adv.php');
	include_once(ROOT_PATH . 'includes/lib_passport.php');		
//	$_REQUEST['access_token'] = "52F649E7D05949BCA52B230A4FD391C4";
	
	$result['error']=1;	
	
	$qq=new class_qq_adv();
	if (isset($_REQUEST['access_token'])){
        $res = $qq->get_openid($_REQUEST['access_token']);
		if (isset($res['openid']))
		{
			$row=$qq->get_user_info();	
			if (isset($row['openid']))
			{
				$result['error']=0;
				//查询是否已有账户
				$user_id = $db->getOne("SELECT user_id FROM " .$ecs->table('users'). " WHERE auth_qq = '".$row['openid']."'");
				if ($user_id){
					//直接登录
					$result['state']=1;
					$result['uinfo']=get_user_json($user_id,$row['openid'],1);		
					$result['msg']="登录成功";	
				}
				else{
					//新用户注册
					$result['state']=2;					
					$username = $row['openid'];
					$password = $row['openid'];					
					$uu = app_register($username, $password);
					if ($uu['error']==0)			
					{
						$sql=" update ".$GLOBALS['ecs']->table('users')." set auth_qq='".$row['openid']."' where user_name = '".$row['openid']."'";
						$GLOBALS['db']->query($sql);
						//填充用户信息						
						$user_id = $db->getOne("SELECT user_id FROM " .$ecs->table('users'). " where user_name = '".$row['openid']."'");						
						$result['uinfo']=get_user_json($user_id,$username,1);
						($row['gender'] == "男") ? $sex = 0 : $sex = 1;
						$result['uinfo']['user_nick']=$row['nickname'];
						$row['figureurl_qq_2'] ? $result['uinfo']['avatar']=$row['figureurl_qq_2'] : $result['uinfo']['avatar']=$row['figureurl_qq_1'];						
						$result['uinfo']['sex']=$sex;
			
//						var_dump($row);
//						var_dump($row['nickname']);			
					}
					else
					{
						$result['error']=1;
						$result['msg']=$uu["message"];
					}
				}	
			}		
			else
			{
				$result['error']=1;					
				$result['msg']=$row;							
			}						
		}
		else
		{
			$result['msg']=$res;			
		}

    }else{
		$result['msg']="NO ACCESS TOKEN";
    }
	echo json_encode($result);	
}

/* 发送验证码 */
elseif ($act == 'mobileCode')
{
	$mobile = !empty($_GET['mobile']) ? $_GET['mobile'] : '';
	$key = !empty($_GET['key']) ? $_GET['key'] : '';	
	$result = array('error' => 1, 'message' => '');	
	include_once(ROOT_PATH . 'includes/alidayu/TopSdk.php');			
//	$mobile = "15980288673";
//	$mobile = "15980265850";
			
	if ($mobile)
	{//验证手机号码有效性
		$time = gmtime();
		$code = generate_code();
		$sms_sign = "速尔跑步";						
		$sms_temp = "SMS_5365735";	
		$sms_parm = '{"code":"'.$code.'","product":"速尔跑步"}';	
		//		
		$client = new ClusterTopClient("23327937","c22a80ecd1c72fcdffeee4c002cfb8da");
		$req = new AlibabaAliqinFcSmsNumSendRequest;
		$req->setExtend("123456");
		$req->setSmsType("normal");
		$req->setSmsFreeSignName($sms_sign);
		$req->setSmsParam($sms_parm);
		$req->setRecNum($mobile);
		$req->setSmsTemplateCode($sms_temp);
		$resp = $client->execute($req);
//		var_dump($resp);					
		
		if (isset($resp->result))
		{
			$result['error'] = 0;				
			$result['message'] = $resp;									
			//短信发送记录
			$result_json = json_encode($resp->result);
			$sql=" insert ".$GLOBALS['ecs']->table('mobile_verify_list')." (mobile,bind_verify,result_json,verify_create_time,state) values ('$mobile','$code','$result_json','$time',1)";
			$GLOBALS['db']->query($sql);
			//短信验证码记录
			$sql=" insert ".$GLOBALS['ecs']->table('sms_send_list')." (mobile,bind_verify,content,sms_sign,sms_temp,create_time) values ('$mobile','$code','$sms_parm','$sms_sign','$sms_temp','$time')";
			$GLOBALS['db']->query($sql);			
		}
		else
		{
			$result['error'] = 1;							
			$result['message'] = $resp;			
			//短信发送记录
			$result_json = json_encode($resp);
			$sql=" insert ".$GLOBALS['ecs']->table('mobile_verify_list')." (mobile,bind_verify,result_json,verify_create_time,state) values ('$mobile','$code','$result_json','$time',0)";
			$GLOBALS['db']->query($sql);			
		}
//		var_dump($code);				
	}
	
	echo json_encode($result);	
}

/* 注册会员协议 */
elseif ($act == 'app_useragreement')
{
	$sql="select * from ".$GLOBALS['ecs']->table('user_agreement')."where id=1 ";
	$Row=$GLOBALS['db']->getRow($sql);
	echo json_encode($Row);
}
/* 注册会员协议 */
elseif ($act == 'app_feedback')
{
//	user_id,message,add_time,email,qq,phone
	$uid = !empty($_GET['uid']) ? $_GET['uid'] : '';
	$message = !empty($_GET['message']) ? $_GET['message'] : '';
	$email = !empty($_GET['email']) ? $_GET['email'] : '';
	$qq = !empty($_GET['qq']) ? $_GET['qq'] : '';
	$phone = !empty($_GET['phone']) ? $_GET['phone'] : '';
	$add_time=gmtime();
	$sql="insert into ".$GLOBALS['ecs']->table('user_feedback')."(user_id,message,add_time,email,qq,phone) values ('$uid','$message','$add_time','$email','$qq','$phone')";
	
	if($GLOBALS['db']->query($sql)){
		$uu['error']=0;
	}else{
		$uu['error']=1;
	}
	echo json_encode($uu);
//	echo json_encode($Row);
}
/* 注册会员的处理 */
elseif ($act == 'app_register')
{
		include_once(ROOT_PATH . 'includes/lib_passport.php');

		$phone = isset($_GET['phone']) ? $_GET['phone'] : '';
		$password = isset($_GET['pwd']) ? $_GET['pwd'] : '';
		$code = isset($_GET['code']) ? $_GET['code'] : '';		

		$verify = $db->getOne("SELECT id FROM " .$ecs->table('sms_send_list'). " where mobile = '".$phone."' and used = 0 and bind_verify = '".$code."'");
//		var_dump($verify);			
		if(!$verify)
		{
			$uu['error']=1;
			$uu['message']="注册失败，验证码错误";
			echo json_encode($uu);
			return;
		}		
		$result = app_register($phone, $password);
		if ($result['error']==0)
		{
			//更新验证码使用情况
			$sql=" update ".$GLOBALS['ecs']->table('sms_send_list')." set used=1 where id = '".$verify."'";
			$GLOBALS['db']->query($sql);			
			//数据返回
			$uu['error']=0;
//			$uu['message']="注册成功";
			$uu['uinfo']=get_user_json($db->getOne("SELECT user_id FROM " .$ecs->table('users'). " where user_name = '".$phone."'"),$phone,1);
		}else{
			$uu['error']=1;
			$uu['message']=$result["message"];
		}
		echo json_encode($uu);
		
}
elseif ($act == 'modify_avatar')
{
	if (!empty($_FILES["file"]["name"]))
	{ //提取文件域内容名称，并判断
//		$path="uppic/"; //上传路径 //上传路径
//		if(!file_exists($path))
//		{
//		//检查是否有该文件夹，如果没有就创建，并给予最高权限
//			mkdir("$path", 0700);
//		}//END IF
		//允许上传的文件格式
		$tp = array("image/gif","image/png","image/jpeg");
		//检查上传文件是否在允许上传的类型
		if(!in_array($_FILES["file"]["type"],$tp))
		{
			$uu['error']=1;
			$uu['message']="图片格式错误";			
			echo json_encode($uu);			
			exit;
		}
		$filetype = $_FILES['file']['type'];
		if($filetype == 'image/jpeg')
		{
			$type = '.jpg';
		}
		if ($filetype == 'image/jpg')
		{
			$type = '.jpg';
		}
		if ($filetype == 'image/png')
		{
			$type = '.jpg';
		}
		if($filetype == 'image/gif')
		{
			$type = '.gif';
		}
		$user_id = $_REQUEST["user_id"];
		$time = gmtime(); //获取时间并赋值给变量
		$path="avatar/"; //上传路径	
		$img = $path.$user_id.$time.$type; //图片的完整路径		
		/* 上传图片到oss*/			
		uploadFile($ossClient, $bucket, $img, $_FILES['file']['tmp_name'],$_FILES['file']['type']);							
		$sql=" update ".$GLOBALS['ecs']->table('users')." set avatar='".$img."' where user_id = ".$user_id;
		$GLOBALS['db']->query($sql);			

		$uu['error']=0;
//		$uu['message']="头像修改成功";
		$uu['avatar']=OSS_HOST.$img;		
		echo json_encode($uu);			
	}//END IF
	else
	{
		$uu['error']=1;
		$uu['message']="头像修改失败";
		echo json_encode($uu);			
	}
}

elseif ($act == 'auth_avatar')
{
	$url = isset($_GET['url']) ? $_GET['url'] : '';
	$user_id = isset($_GET['user_id']) ? $_GET['user_id'] : 0;
//	$url = "http://wx.qlogo.cn/mmopen/ajNVdqHZLLAHFZ95tzZAOsD6nibYE43rzlQyJAPsOKbn6icIzgUo9o0SYUNa77BeyA4QxF7ibrUgp8ZqU9DyF8EVQ/64";
	$img = get_url_image($url,$user_id);
	if($img)
	{
		uploadFile($ossClient, $bucket, $img, ROOT_PATH.$img);			
		$sql=" update ".$GLOBALS['ecs']->table('users')." set avatar='".$img."' where user_id = ".$user_id;
		$GLOBALS['db']->query($sql);			
		
		$uu['error']=0;
//		$uu['message']="头像修改失败";
		$uu['avatar']=OSS_HOST.$img;	
		echo json_encode($uu);			
	}		
}

/* 信息的处理 */
elseif ($act == 'app_information')
{
		$nick = isset($_GET['unick']) ? $_GET['unick'] : '';
		$uname = isset($_GET['uname']) ? $_GET['uname'] : '';		
		$sex = isset($_GET['usex']) ? $_GET['usex'] : 0;
		$id = isset($_GET['uid']) ? $_GET['uid'] : 0;
		$date = isset($_GET['date']) ? $_GET['date'] : 0;
		$weight = isset($_GET['weight']) ? $_GET['weight'] : 0;
		$hight = isset($_GET['hight']) ? $_GET['hight'] : 0;
		$city = isset($_GET['city']) ? $_GET['city'] : '';
		$province = isset($_GET['province']) ? $_GET['province'] : '';
		$sql ="update ".$GLOBALS['ecs']->table('users')." set user_nick = '$nick' ,sex='$sex' ,step=1 ,birthday='$date' ,weight='$weight' ,hight='$hight' ,city='$city' ,province='$province' ".
		"where user_id = '$id' ";
		$GLOBALS['db']->query($sql);
		if($GLOBALS['db']->query($sql)){
//			$sql="select user_name,user_nick,sex,user_id from".$GLOBALS['ecs']->table('users')."  where user_id = '$id' ";
//			$uu=$db->getrow($sql);
			$uu['error']=0;
			$uu['message']="注册成功";
			$uu['uinfo']=get_user_json($id,$uname,1);			
		}else{
			$uu['error']=1;
			$uu['message']="注册失败";
		}
		echo json_encode($uu);
}

/* 添加收藏商品(ajax) */
elseif ($act == 'collect')
{
	include_once(ROOT_PATH .'includes/cls_json.php');
	$json = new JSON();
	$result = array('error' => 0, 'message' => '');
	$goods_id = $_GET['id'];

	if (!isset($_SESSION['user_id']) || $_SESSION['user_id'] == 0)
	{
		$result['error'] = 1;
		$result['message'] = "由于您还没有登录，因此您还不能使用该功能。";
		die($json->encode($result));
	}
	else
	{
		/* 检查是否已经存在于用户的收藏夹 */
		$sql = "SELECT COUNT(*) FROM " .$GLOBALS['ecs']->table('collect_goods') .
			" WHERE user_id='$_SESSION[user_id]' AND goods_id = '$goods_id'";
		if ($GLOBALS['db']->GetOne($sql) > 0)
		{
			$result['error'] = 1;
			$result['message'] = "该商品已经存在于您的收藏夹中。";
			die($json->encode($result));
		}
		else
		{
			$time = gmtime();
			$sql = "INSERT INTO " .$GLOBALS['ecs']->table('collect_goods'). " (user_id, goods_id, add_time)" .
					"VALUES ('$_SESSION[user_id]', '$goods_id', '$time')";

			if ($GLOBALS['db']->query($sql) === false)
			{
				$result['error'] = 1;
				$result['message'] = $GLOBALS['db']->errorMsg();
				die($json->encode($result));
			}
			else
			{
				$result['error'] = 0;
				$result['message'] = "该商品已经成功地加入了您的收藏夹。";
				die($json->encode($result));
			}
		}
	}
}

/* 用户中心 */
else
{
//	if ($_SESSION['user_id'] > 0)
//	{
//		show_user_center();
//	}
//	else
//	{
//		$reuser_name= isset($_COOKIE['ECS']['reuser_name']) ? $_COOKIE['ECS']['reuser_name'] : '';
//		if(!empty($reuser_name)){
//			$smarty->assign('reuser_name', $reuser_name);
//		}
//		$smarty->assign('footer', get_footer());
//		$smarty->display('login.dwt');
//		exit;
//	}
}

/* 
 * 手机验证码生成函数
*/
function generate_code($length = 6) {
	return rand(pow(10, ($length - 1)), pow(10, $length) - 1);
}


function get_url_image($url,$pre="") {
//	$ext = strtolower(end(explode('.', $url)));
//	if ($ext != "gif" && $ext != "jpg" && $ext != "png" && $ext != "bmp" && $ext != "jpeg") {
//		return $url;
//	}
	$img = curls($url);	
	$name = gmtime(); //获取时间并赋值给变量		
	$ext = ".jpg";	
//	$target = ROOT_PATH . DATA_DIR . '/afficheimg/' . $name;
	$path = 'avatar/';
	$tmp_file = $path. $pre. $name. $ext;
	$filename = ROOT_PATH . $tmp_file;
//	if(!file_exists($path))
//	{
//	//检查是否有该文件夹，如果没有就创建，并给予最高权限
//		mkdir("$path", 0700);
//	}	
//	var_dump($filename);
	
	$fp = @fopen($filename, "a");
	fwrite($fp, $img);
	fclose($fp);
		
	return $tmp_file;
}

function curls($url, $timeout = '10')  
{  
    // 1. 初始化  
    $ch = curl_init();  
    // 2. 设置选项，包括URL  
    curl_setopt($ch, CURLOPT_URL, $url);  
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
    curl_setopt($ch, CURLOPT_HEADER, 0);  
    // 3. 执行并获取HTML文档内容  
    $info = curl_exec($ch);  
    // 4. 释放curl句柄  
    curl_close($ch);  
  
    return $info;  
}
/**
 * 用户中心显示
 */
//function show_user_center()
//{
//  include_once(ROOT_PATH .'includes/lib_clips.php');
//	$best_goods = get_recommend_goods('best');
//	if (count($best_goods) > 0)
//	{
//		foreach  ($best_goods as $key => $best_data)
//		{
//			$best_goods[$key]['shop_price'] = encode_output($best_data['shop_price']);
//			$best_goods[$key]['name'] = encode_output($best_data['name']);
//		}
//	}
//	//22:18 2013-7-16
//	$rank_name = $GLOBALS['db']->getOne('SELECT rank_name FROM ' . $GLOBALS['ecs']->table('user_rank') . ' WHERE rank_id = '.$_SESSION['user_rank']);
//	$GLOBALS['smarty']->assign('info', get_user_default($_SESSION['user_id']));
//	$GLOBALS['smarty']->assign('rank_name', $rank_name);
//	$GLOBALS['smarty']->assign('user_info', get_user_info());
//	$GLOBALS['smarty']->assign('best_goods' , $best_goods);
//	$GLOBALS['smarty']->assign('footer', get_footer());
//	$GLOBALS['smarty']->display('user.dwt');
//}

/* 
 * 自动登录账户密码验证
*/
function check_auto_login($login_name, $pwd) {
	$user_id = $GLOBALS['db']->getOne("select user_id from".$GLOBALS['ecs']->table('users')
					  ." where (user_name = '$login_name' or auth_wechat = '$login_name' or auth_qq = '$login_name') and pwd_auto = '$pwd'")	;
	return $user_id;
}

/* 
 * 返回给手机端用户信息
 *
 * @access  public
 * @param   string  $username   用户名
 *
 * @return  int
*/
function get_user_json($user_id,$user_name,$update=0) {
	
	$sql="select user_nick,sex,pwd_auto as pwd,avatar,city,province,hight,weight,birthday from".$GLOBALS['ecs']->table('users')." where user_id = '$user_id'";
	$uinfo=$GLOBALS['db']->getRow($sql);
	//更换自动登录pwd
	if ($update)
	{
		if (!$uinfo['pwd'])
		{
			$uinfo['pwd'] = $GLOBALS['db']->getOne("select password from".$GLOBALS['ecs']->table('users')." where user_id = '$user_id'");
		}
		$uinfo['pwd'] = md5($uinfo['pwd']);
		$sql=" update ".$GLOBALS['ecs']->table('users')." set pwd_auto='".$uinfo['pwd']."' where user_id = '$user_id'";
		$GLOBALS['db']->query($sql);				
	}
	if ($uinfo['avatar'])
	{
		$uinfo['avatar']=OSS_HOST.$uinfo['avatar'];	
	}
	$uinfo['user_id'] = $user_id;
	$uinfo['user_name'] = $user_name;	
	return $uinfo;
}
		
/**
 * app注册
 */
function app_register($username, $password)
{
	/* 检查username */
	if (empty($username))
	{
		$uu['message']='用户名必须填写';
		$uu['error']=1;		
		return $uu;
	}
	
	if (preg_match('/\'\/^\\s*$|^c:\\\\con\\\\con$|[%,\\*\\"\\s\\t\\<\\>\\&\'\\\\]/', $username))
	{
		$uu['message']='用户名错误';
		$uu['error']=1;
		return $uu;
	}

	/* 检查是否和其他用户重名 */
	if ($GLOBALS['db']->getOne("SELECT user_id FROM " .$GLOBALS['ecs']->table('users'). " WHERE user_name = '".$username."'"))
	{
		$uu['message']='此用户已存在';
		$uu['error']=1;
		return $uu;
	}
	
	/* 检查是否和管理员重名 */
	if (admin_registered($username))
	{
		$uu['message']='此用户已存在';
		$uu['error']=1;
		return $uu;
	}
	
	$email = $username."@sure.cn";
	if (!$GLOBALS['user']->add_user($username, $password, $email))
	{
		$uu['message']='注册失败';
		$uu['error']=1;
		return $uu;
	}
	else
	{
		//注册成功
//		$uu['username']=$username;
//		$uu['error']=0;
//		echo json_encode($uu);
//		$sql="select user_id from".$GLOBALS['ecs']->table('users')." where user_name = '$user_name'";
//		$user_id=$db->getOne($sql);
//		$uu['username']=$user_name;
//		$uu['user_id']=$user_id;
//		$uu['error']=0;
//		echo json_encode($uu);
//		/* 设置成登录状态 */
//		$GLOBALS['user']->set_session($username);
//		$GLOBALS['user']->set_cookie($username);
	}

//		//定义other合法的变量数组
//		$other_key_array = array('msn', 'qq', 'office_phone', 'home_phone', 'mobile_phone');
//		$update_data['reg_time'] = local_strtotime(local_date('Y-m-d H:i:s'));
//		if ($other)
//		{
//			foreach ($other as $key=>$val)
//			{
//				//删除非法key值
//				if (!in_array($key, $other_key_array))
//				{
//					unset($other[$key]);
//				}
//				else
//				{
//					$other[$key] =  htmlspecialchars(trim($val)); //防止用户输入javascript代码
//				}
//			}
//			$update_data = array_merge($update_data, $other);
//		}
//		$GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('users'), $update_data, 'UPDATE', 'user_id = ' . $_SESSION['user_id']);
//
//		update_user_info();	  // 更新用户信息
//      $Loaction = 'user.php?act=user_center';
//      ecs_header("Location: $Loaction\n");
		$uu['error']=0;
		return $uu;
}

/**
 * 手机注册
 */
function m_register($username, $password, $email, $other = array())
{
	/* 检查username */
	if (empty($username))
	{
		echo '<script>alert("用户名必须填写！");window.location.href="user.php?act=register"; </script>';
		return false;
	}
	if (preg_match('/\'\/^\\s*$|^c:\\\\con\\\\con$|[%,\\*\\"\\s\\t\\<\\>\\&\'\\\\]/', $username))
	{
		echo '<script>alert("用户名错误！");window.location.href="user.php?act=register"; </script>';
		return false;
	}

	/* 检查是否和管理员重名 */
	if (admin_registered($username))
	{
		echo '<script>alert("此用户已存在！");window.location.href="user.php?act=register"; </script>';
		return false;
	}

	if (!$GLOBALS['user']->add_user($username, $password, $email))
	{
		echo '<script>alert("注册失败！");window.location.href="user.php?act=register"; </script>';
		//注册失败
		return false;
	}
	else
	{
		//注册成功

		/* 设置成登录状态 */
		$GLOBALS['user']->set_session($username);
		$GLOBALS['user']->set_cookie($username);
	}

		//定义other合法的变量数组
		$other_key_array = array('msn', 'qq', 'office_phone', 'home_phone', 'mobile_phone');
		$update_data['reg_time'] = local_strtotime(local_date('Y-m-d H:i:s'));
		if ($other)
		{
			foreach ($other as $key=>$val)
			{
				//删除非法key值
				if (!in_array($key, $other_key_array))
				{
					unset($other[$key]);
				}
				else
				{
					$other[$key] =  htmlspecialchars(trim($val)); //防止用户输入javascript代码
				}
			}
			$update_data = array_merge($update_data, $other);
		}
		$GLOBALS['db']->autoExecute($GLOBALS['ecs']->table('users'), $update_data, 'UPDATE', 'user_id = ' . $_SESSION['user_id']);

		update_user_info();	  // 更新用户信息
        $Loaction = 'user.php?act=user_center';
        ecs_header("Location: $Loaction\n");
		return true;

}
?>